<!DOCTYPE html>
<html lang="zh-CN">
  <head><meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>


<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />

<meta name="theme-color" content="#f8f5ec" />
<meta name="msapplication-navbutton-color" content="#f8f5ec">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="#f8f5ec">

<meta name="description" content="Trilium API二次开发"/><link rel="alternate" href="/default" title="Hexo" ><link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=2.11.1" />
<link rel="canonical" href="https://y_kvm.gitee.io/2022/02/21/Trilium API二次开发/"/>

<link rel="stylesheet" type="text/css" href="/lib/fancybox/jquery.fancybox.css" /><script type="text/x-mathjax-config">
    MathJax.Hub.Config({ tex2jax: { inlineMath: [['$','$'], ['\\(','\\)']] } });
  </script>
  <script type="text/javascript" async src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-MML-AM_CHTML"></script>
<link rel="stylesheet" type="text/css" href="/css/style.css?v=2.11.1" />

<script id="baidu_analytics">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?9c3376609b4eb87c460cc9503d43f10e";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script><script src="//cdn1.lncld.net/static/js/3.1.1/av-min.js"></script>
  <script id="leancloud">
    AV.init({
      appId: "mhfJYrvbNrJ7wa3pFNWptaoe-MdYXbMMI",
      appKey: "IDppcTL9Rek0zfezfOeuICaF"
    });
  </script><script>
  window.config = {"leancloud":{"app_id":"mhfJYrvbNrJ7wa3pFNWptaoe-MdYXbMMI","app_key":"IDppcTL9Rek0zfezfOeuICaF"},"toc":true,"fancybox":true,"pjax":"","latex":true};
</script>

    <title>Trilium API二次开发 - Hexo</title>
  <meta name="generator" content="Hexo 5.4.1"></head>

  <body><div id="mobile-navbar" class="mobile-navbar">
  <div class="mobile-header-logo">
    <a href="/." class="logo">Hexo</a>
  </div>
  <div class="mobile-navbar-icon">
    <span></span>
    <span></span>
    <span></span>
  </div>
</div>

<nav id="mobile-menu" class="mobile-menu slideout-menu">
  <ul class="mobile-menu-list"><a href="/">
        <li class="mobile-menu-item">首页
          </li>
      </a><a href="/tags/">
        <li class="mobile-menu-item">标签
          </li>
      </a><a href="/about/">
        <li class="mobile-menu-item">关于
          </li>
      </a></ul>
</nav>
<div class="container" id="mobile-panel">
      <header id="header" class="header"><div class="logo-wrapper">
  <a href="/." class="logo">Hexo</a>
</div>

<nav class="site-navbar"><ul id="menu" class="menu"><li class="menu-item">
          <a class="menu-item-link" href="/">
            首页
            </a>
        </li>
      <li class="menu-item">
          <a class="menu-item-link" href="/tags/">
            标签
            </a>
        </li>
      <li class="menu-item">
          <a class="menu-item-link" href="/about/">
            关于
            </a>
        </li>
      </ul></nav>
</header>

      <main id="main" class="main">
        <div class="content-wrapper">
          <div id="content" class="content"><article class="post">
    <header class="post-header">
      <h1 class="post-title">Trilium API二次开发
        </h1>

      <div class="post-meta">
        <span class="post-time">
          2022-02-21
        </span><span class="post-visits"
             data-url="/2022/02/21/Trilium%20API%E4%BA%8C%E6%AC%A1%E5%BC%80%E5%8F%91/"
             data-title="Trilium API二次开发">
          阅读次数 0
        </span>
        </div>
    </header>

    <div class="post-toc" id="post-toc">
    <h2 class="post-toc-title">文章目录</h2>
    <div class="post-toc-content">
      <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%9B%AE%E6%A0%87"><span class="toc-text">目标</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%A0%B7%E4%BE%8B%E4%BB%A3%E7%A0%81"><span class="toc-text">样例代码</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BB%A3%E7%A0%81%E5%88%86%E6%9E%90"><span class="toc-text">代码分析</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%AE%98%E6%96%B9API"><span class="toc-text">官方API</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#getTodayNote-%E2%86%92-Promise"><span class="toc-text">getTodayNote() → {Promise.}</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#Returns"><span class="toc-text">Returns:</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#getWeekNote-date-options-%E2%86%92-Note-null"><span class="toc-text">getWeekNote(date, options) → {Note|null}</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#Parameters"><span class="toc-text">Parameters:</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#Returns-1"><span class="toc-text">Returns:</span></a></li></ol></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%AE%9E%E6%93%8D"><span class="toc-text">实操</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Lesson"><span class="toc-text">Lesson</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%8F%82%E8%80%83%E8%B5%84%E6%96%99"><span class="toc-text">参考资料</span></a></li></ol>
    </div>
  </div><div class="post-content"><blockquote>
<p>Trilium Notes是一个分层的笔记应用程序，专注于建立大型个人知识库。</p>
<p>github： <a target="_blank" rel="noopener" href="https://github.com/zadam/trilium">zadam/trilium: Build your personal knowledge base with Trilium Notes (github.com)</a></p>
</blockquote>
<h2 id="目标"><a href="#目标" class="headerlink" title="目标"></a>目标</h2><p>trilium提供了高度可子定制的接口实现，可自行进行各种DIY设计，就是有些费手😂</p>
<p>今天需要添加一个定期总结的日记功能，在原有基础上希望能生成周结而非日结。</p>
<h2 id="样例代码"><a href="#样例代码" class="headerlink" title="样例代码"></a>样例代码</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 今天按钮, 在日记下新增一个今天的笔记</span></span><br><span class="line"></span><br><span class="line">api.<span class="title function_">addButtonToToolbar</span>(&#123;</span><br><span class="line">    <span class="attr">title</span>: <span class="string">&#x27;今天&#x27;</span>,</span><br><span class="line">    <span class="attr">icon</span>: <span class="string">&#x27;calendar-star&#x27;</span>,</span><br><span class="line">    <span class="comment">// 快捷键</span></span><br><span class="line">    <span class="attr">shortcut</span>: <span class="string">&#x27;alt+t&#x27;</span>,</span><br><span class="line">    <span class="comment">// 触发的异步函数</span></span><br><span class="line">    <span class="attr">action</span>: <span class="keyword">async</span> <span class="keyword">function</span>(<span class="params"></span>) &#123;</span><br><span class="line">        <span class="keyword">const</span> todayNote = <span class="keyword">await</span> api.<span class="title function_">getTodayNote</span>();</span><br><span class="line">        <span class="keyword">await</span> api.<span class="title function_">waitUntilSynced</span>();</span><br><span class="line">        api.<span class="title function_">activateNote</span>(weekNote.<span class="property">noteId</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>

<h2 id="代码分析"><a href="#代码分析" class="headerlink" title="代码分析"></a>代码分析</h2><p>我从未学过js但是根据其他语言的学习和特点我们可以大胆猜测以下，这个api的功能。</p>
<p>首先这里调用了作者自定义的API，具有api作为前缀以和其他js的内容进行区分。</p>
<p>title，icon，shortcut如上字面意思不过多展开</p>
<p>action 这里定义了一个异步函数并以async进行修饰，内部采用await进行标识。简单的来说，这类函数当执行时，耗时操作不占用主线程遇到耗时操作（如await）自动挂起。继续在调用函数往下执行。当await部分执行完成后再唤醒继续执行后续内容，这点和C#的异步编程基本类似。</p>
<p>函数内部定义也很简单，根据当前日期获取/创造一个日记，跳转到这个日记。</p>
<h2 id="官方API"><a href="#官方API" class="headerlink" title="官方API"></a>官方API</h2><h4 id="getTodayNote-→-Promise"><a href="#getTodayNote-→-Promise" class="headerlink" title="getTodayNote() → {Promise.}"></a>getTodayNote() → {Promise.<NoteShort>}</NoteShort></h4><p>Returns date-note for today. If it doesn’t exist, it is automatically created.</p>
<blockquote>
<p> Source: <a target="_blank" rel="noopener" href="https://zadam.github.io/trilium/frontend_api/services_frontend_script_api.js.html">services/frontend_script_api.js</a>, <a target="_blank" rel="noopener" href="https://zadam.github.io/trilium/frontend_api/services_frontend_script_api.js.html#line389">line 389</a></p>
</blockquote>
<h5 id="Returns"><a href="#Returns" class="headerlink" title="Returns:"></a>Returns:</h5><ul>
<li><p>Type</p>
<p>Promise.<NoteShort></NoteShort></p>
</li>
</ul>
<h4 id="getWeekNote-date-options-→-Note-null"><a href="#getWeekNote-date-options-→-Note-null" class="headerlink" title="getWeekNote(date, options) → {Note|null}"></a>getWeekNote(date, options) → {<a target="_blank" rel="noopener" href="https://zadam.github.io/trilium/backend_api/Note.html">Note</a>|null}</h4><p>Returns note for the first date of the week of the given date.</p>
<h5 id="Parameters"><a href="#Parameters" class="headerlink" title="Parameters:"></a>Parameters:</h5><table>
<thead>
<tr>
<th align="left">Name</th>
<th align="left">Type</th>
<th align="left">Description</th>
</tr>
</thead>
<tbody><tr>
<td align="left"><code>date</code></td>
<td align="left">string</td>
<td align="left">in YYYY-MM-DD format</td>
</tr>
<tr>
<td align="left"><code>options</code></td>
<td align="left">object</td>
<td align="left">“startOfTheWeek” - either “monday” (default) or “sunday”</td>
</tr>
</tbody></table>
<blockquote>
<p>Source:<a target="_blank" rel="noopener" href="https://zadam.github.io/trilium/backend_api/services_backend_script_api.js.html">services/backend_script_api.js</a>, <a target="_blank" rel="noopener" href="https://zadam.github.io/trilium/backend_api/services_backend_script_api.js.html#line341">line 341</a></p>
</blockquote>
<h5 id="Returns-1"><a href="#Returns-1" class="headerlink" title="Returns:"></a>Returns:</h5><ul>
<li><p>Type</p>
<p><a target="_blank" rel="noopener" href="https://zadam.github.io/trilium/backend_api/Note.html">Note</a> | null</p>
</li>
</ul>
<h2 id="实操"><a href="#实操" class="headerlink" title="实操"></a>实操</h2><p>可以看到和Today区别在于我们需要自己去获取日期，然而官方API也引入了dayjs库，用于获取日期和格式化日期。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> date = <span class="keyword">await</span> <span class="title function_">dayjs</span>().<span class="title function_">format</span>(<span class="string">&#x27;YYYY-MM-DD&#x27;</span>);</span><br></pre></td></tr></table></figure>

<p>但是，我们建立的是周末总结，官方的endOf采用周六最为一周最后一天，因而我们做出如下更改哦</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> date = <span class="keyword">await</span> <span class="title function_">dayjs</span>().<span class="title function_">subtract</span>(<span class="number">1</span>, <span class="string">&#x27;day&#x27;</span>).<span class="title function_">endOf</span>(<span class="string">&#x27;week&#x27;</span>).<span class="title function_">add</span>(<span class="number">1</span>,<span class="string">&#x27;day&#x27;</span>).<span class="title function_">format</span>(<span class="string">&#x27;YYYY-MM-DD&#x27;</span>);</span><br></pre></td></tr></table></figure>

<p>最终代码</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 本周总结按钮, 在日记下新增一个本周的笔记</span></span><br><span class="line"></span><br><span class="line">api.<span class="title function_">addButtonToToolbar</span>(&#123;</span><br><span class="line">    <span class="attr">title</span>: <span class="string">&#x27;本周总结&#x27;</span>,</span><br><span class="line">    <span class="attr">icon</span>: <span class="string">&#x27;calendar-star&#x27;</span>,</span><br><span class="line">    <span class="comment">// 快捷键</span></span><br><span class="line">    <span class="attr">shortcut</span>: <span class="string">&#x27;alt+t&#x27;</span>,</span><br><span class="line">    <span class="comment">// 触发的异步函数</span></span><br><span class="line">    <span class="attr">action</span>: <span class="keyword">async</span> <span class="keyword">function</span>(<span class="params"></span>) &#123;</span><br><span class="line">        <span class="keyword">const</span> date = <span class="keyword">await</span> <span class="title function_">dayjs</span>().<span class="title function_">subtract</span>(<span class="number">1</span>, <span class="string">&#x27;day&#x27;</span>).<span class="title function_">endOf</span>(<span class="string">&#x27;week&#x27;</span>).<span class="title function_">add</span>(<span class="number">1</span>,<span class="string">&#x27;day&#x27;</span>).<span class="title function_">format</span>(<span class="string">&#x27;YYYY-MM-DD&#x27;</span>);</span><br><span class="line">        <span class="keyword">const</span> weekNote = <span class="keyword">await</span> api.<span class="title function_">getDateNote</span>(date);</span><br><span class="line">        <span class="keyword">await</span> api.<span class="title function_">waitUntilSynced</span>();</span><br><span class="line">        <span class="variable language_">console</span>.<span class="title function_">log</span>(date);</span><br><span class="line">        api.<span class="title function_">activateNote</span>(weekNote.<span class="property">noteId</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>

<h2 id="Lesson"><a href="#Lesson" class="headerlink" title="Lesson"></a>Lesson</h2><p>编程语言大多存在共同之处，掌握一门编程语言后，即可迅速上手其他语言，因而我们需要的不是掌握多门编程语言，而是学深学精一门语言，达到别人很难替代的地步，在此基础上我们才能跟得上时代的浪潮而不被淘汰。</p>
<h2 id="参考资料"><a href="#参考资料" class="headerlink" title="参考资料"></a>参考资料</h2><p><a target="_blank" rel="noopener" href="https://juejin.cn/post/6933548549321785352">dayjs-入门及常用方法 - 掘金 (juejin.cn)</a></p>
<p><a target="_blank" rel="noopener" href="https://github.com/zadam/trilium/issues/1323">FR: Yesterday / Tomorrow buttons · Issue #1323 · zadam/trilium (github.com)</a></p>

      </div>
      <div class="post-copyright">
    <p class="copyright-item">
      <span>原文作者: </span>
      <a href="https://y_kvm.gitee.io">Explorer</a>
    </p>
    <p class="copyright-item">
      <span>原文链接: </span>
      <a href="https://y_kvm.gitee.io/2022/02/21/Trilium%20API%E4%BA%8C%E6%AC%A1%E5%BC%80%E5%8F%91/">https://y_kvm.gitee.io/2022/02/21/Trilium%20API%E4%BA%8C%E6%AC%A1%E5%BC%80%E5%8F%91/</a>
    </p>
    <p class="copyright-item">
      <span>许可协议: </span><a rel="license" href="http://creativecommons.org/licenses/by-nc/4.0/" target="_blank">知识共享署名-非商业性使用 4.0 国际许可协议</a>
    </p>
  </div>
      <footer class="post-footer">
        
        <nav class="post-nav"><a class="prev" href="/2022/02/21/%E5%88%9B%E5%BB%BA%E5%BF%AB%E6%8D%B7%E5%90%AF%E5%8A%A8/">
        <i class="iconfont icon-left"></i>
        <span class="prev-text nav-default">创建快捷启动</span>
        <span class="prev-text nav-mobile">上一篇</span>
      </a>
    <a class="next" href="/2022/02/07/index/">
        <span class="next-text nav-default">index</span>
        <span class="prev-text nav-mobile">下一篇</span>
        <i class="iconfont icon-right"></i>
      </a>
    </nav></footer>
    </article></div><div class="comments" id="comments"></div></div>
      </main>

      <footer id="footer" class="footer"><div class="social-links"><a href="mailto:alternatingcurrenttesla@outlook.com" class="iconfont icon-email" title="email"></a>
        <a target="_blank" rel="noopener" href="https://github.com/KVM-Explorer" class="iconfont icon-github" title="github"></a>
        <a href="/atom.xml" class="iconfont icon-rss" title="rss"></a>
    </div><div class="copyright">
  <span class="power-by">
    由 <a class="hexo-link" target="_blank" rel="noopener" href="https://hexo.io/">Hexo</a> 强力驱动
  </span>
  <span class="division">|</span>
  <span class="theme-info">
    主题 - 
    <a class="theme-link" target="_blank" rel="noopener" href="https://github.com/ahonn/hexo-theme-even">Even</a>
  </span>

  <span class="copyright-year">&copy;2015 - 2022<span class="heart">
      <i class="iconfont icon-heart"></i>
    </span>
    <span class="author">Explorer</span>
  </span>
</div>
</footer>

      <div class="back-to-top" id="back-to-top">
        <i class="iconfont icon-up"></i>
      </div>
    </div><script type="text/javascript" src="/lib/jquery/jquery.min.js"></script>
  <script type="text/javascript" src="/lib/slideout/slideout.js"></script>
  <script type="text/javascript" src="/lib/fancybox/jquery.fancybox.pack.js"></script>
  <script type="text/javascript" src="/js/src/even.js?v=2.11.1"></script>
</body>
</html>
